package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.chronicdis.*;
import com.gxuwz.medical.vo.PageBean;

public class ChronicdisDao extends GenericDao<Chronicdis>{
	
	@Override
	protected Chronicdis handle(ResultSet rs) throws SQLException {	
		Chronicdis c=new Chronicdis();
		c.setIllcode(rs.getString("illcode"));//疾病编码
		c.setIllname(rs.getString("illname"));//疾病名称
		c.setPycode(rs.getString("pycode"));//拼音码
		c.setWbcode(rs.getString("wbcode"));//五笔码
		return c;
	}
	
	public void add(Chronicdis c) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "insert into chronicdis(illcode,illname) values('"
					+ c.getIllcode() + "','" + c.getIllname() + "')";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("添加慢性病失败 " + e.getMessage(), e);
		}
	}

	public void delete(String illcode) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "delete from chronicdis where illcode='" + illcode + "'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("删除慢性病失败" + e.getMessage(), e);
		}
	}

	public Chronicdis query(String illcode) throws SQLException {
		Chronicdis c = new Chronicdis();
		DbUtil db = new DbUtil();
		Connection conn = DbUtil.getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = null;
		try {
			String sql = "select * from chronicdis where illcode='" + illcode
					+ "'";
			rs = db.executeQuery(stmt, sql);
			if (rs.next()) {
				c=handle(rs);
			}

		} catch (SQLException e) {
			throw new SQLException("查询慢性病失败" + e.getMessage(), e);
		}finally{
			db.close(rs, stmt, conn);
		}
		return c;
	}
	
	public int count() throws SQLException{//返回记录数
		int count = 0;
		DbUtil db = new DbUtil();
		Connection conn = DbUtil.getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = null;
		try {
			String sql = "select count(illcode) from chronicdis";
			rs = stmt.executeQuery(sql);
			if(rs.next())
				count = rs.getInt(1);
		}catch (SQLException e) {
			throw new SQLException("查询记录数量失败" + e.getMessage(), e);
		}finally{
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
			if(conn!=null)
				conn.close();
		}
		return count;
	}

	public PageBean queryAll(int page,int size) throws SQLException {
		PageBean p = new PageBean(page,size,count());
		List list = new ArrayList();
		DbUtil db = new DbUtil();
		Connection conn = DbUtil.getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = null;
		try {
			String sql = "select * from chronicdis limit "+(page-1)*size+","+size+"";
			rs = db.executeQuery(stmt, sql);
			while (rs.next()) {
				Chronicdis c=handle(rs);
				list.add(c);
			}
			p.setPageData(list);
			return p;

		} catch (SQLException e) {
			throw new SQLException("查询全部慢性病失败" + e.getMessage(), e);
		}finally{
			db.close(rs, stmt, conn);
		}
	}
	
	public List<Chronicdis> queryAll() throws SQLException {
		List<Chronicdis> list = new ArrayList<Chronicdis>();
		DbUtil db = new DbUtil();
		Connection conn = DbUtil.getConn();
		Statement stmt = conn.createStatement();
		ResultSet rs = null;
		try {
			String sql = "select * from chronicdis";
			rs = db.executeQuery(stmt, sql);
			while (rs.next()) {
				Chronicdis c = handle(rs);
				list.add(c);
			}
			
			return list;

		} catch (SQLException e) {
			throw new SQLException("查询全部慢性病失败" + e.getMessage(), e);
		}finally{
			db.close(rs, stmt, conn);
		}
	}

	public void update(Chronicdis c) throws SQLException {
		try {
			DbUtil db = new DbUtil();
			String sql = "update chronicdis set illname='"+c.getIllname()+"' where illcode='"+c.getIllcode()+"'";
			db.executeUpdate(sql);
		} catch (SQLException e) {
			throw new SQLException("修改慢性病失败 " + e.getMessage(), e);
		}
	}
	
}
